package com.skcc.ts.nsi.user;

import java.util.ArrayList;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.expense.MEExpenseListSearch;
import com.skcc.ts.nsi.service.SocketClientService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.AdapterView.OnItemSelectedListener;

public class MEUsrInPjtSearch extends Activity {

	private String[] pjtCode;
	private String[] pjtName;
	private Button btn_usrinpjt_list_show;
	private int id = 13;// 기능분류:사용자관리(프로젝트별 팀원조회)
	private String myId;
	private String selectedPjt;
	private String selectedPjtCode;
	private boolean terminate = false;
	private String addr;
	private String port;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meusrinpjtsearch);

		doListPjt();

		initSpinners();

		initButtons();
	}

	private void initButtons() {
		btn_usrinpjt_list_show = (Button) findViewById(R.id.btn_usrinpjt_list_show);
		btn_usrinpjt_list_show.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ("전체".equals(selectedPjt)) {
					new AlertDialog.Builder(MEUsrInPjtSearch.this).setTitle("알림").setMessage("프로젝트를 선택하여 주십시오")
							.setPositiveButton("OK", null).show();
				}
				Intent sendI = new Intent(MEUsrInPjtSearch.this, SocketClientService.class);
				sendI.putExtra("id", id);// 13
				sendI.putExtra("myId", myId);
				sendI.putExtra("loginAddr", addr);
				sendI.putExtra("loginPort", port);
				sendI.putExtra("projectCode", selectedPjtCode);
				sendI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startService(sendI);
				if (terminate) {
					finish();
				}
			}
		});

	}

	private void initSpinners() {
		ArrayAdapter<String> adapter_pjt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, pjtName);

		Spinner sp_pjt = (Spinner) findViewById(R.id.spinner_usrinpjt_pjtlist);
		adapter_pjt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_pjt.setAdapter(adapter_pjt);

		sp_pjt.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedPjt = pjtName[position];
				selectedPjtCode = pjtCode[position];

			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedPjt = "전체";
			}
		});
	}

	private void doListPjt() {
		Intent receiveI = getIntent();
		boolean pjtList = receiveI.getBooleanExtra("teamProject", false);
		if (pjtList) {
			myId = receiveI.getStringExtra("myId");
			pjtCode = receiveI.getStringArrayExtra("projectCode");
			pjtName = receiveI.getStringArrayExtra("projectName");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			terminate = true;
		}
	}

}
